#include <set>
using namespace std;

// Definition for a binary tree node.
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

class Solution {
public:
  void proc(TreeNode *node, set<int> &res) {
    if (node == nullptr) {
      return;
    }
    if (res.find(node->val) == res.end()) {
      res.insert(node->val);
    }
    proc(node->left,res);
    proc(node->right,res);
  }
  int numColor(TreeNode *root) {
    set<int> res;
    proc(root, res);
    return res.size();
  }
};
